﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HouseOfCards;

namespace CardLibTest
{
   public partial class Form1 : Form
   {
      Deck _deck = new Deck();
      Boolean _cell1;
      Boolean _cell2;
      Boolean _cell3;
      Boolean _cell4;
      Boolean _cell5;

      public Form1()
      {
         InitializeComponent();
         _deck.Shuffle();
      }

      private void btnTest_Click(object sender, EventArgs e)
      {
         Card card = new Card(CardValues.CardBack);

         try
         {
            try
            {
               card = _deck.GetNextCard();
            }
            catch (DeckEmptyException ex)
            {
               lblError.Text = ex.Message;
               //re-shuffle the deck
               _deck.Shuffle();

               cardImage1.SetCardValue(CardValues.CardBack);
               cardImage2.SetCardValue(CardValues.CardBack);
               cardImage3.SetCardValue(CardValues.CardBack);
               cardImage4.SetCardValue(CardValues.CardBack);
               cardImage5.SetCardValue(CardValues.CardBack);

               return;
            }

            lblError.Text = "";

            if (_cell1 && _cell2 && _cell3 && _cell4 && _cell5)
            {
               ResetCells();
            }

            if (!_cell1)
            {
               this.cardImage1.CardValue = card.Value;
               _cell1 = true;
               lblCell1.Text = this.cardImage1.GetFaceValue().ToString() + " of " + this.cardImage1.GetSuitValue().ToString();
            }
            else if (!_cell2)
            {
               this.cardImage2.CardValue = card.Value;
               _cell2 = true;
               lblCell2.Text = this.cardImage2.GetFaceValue().ToString() + " of " + this.cardImage2.GetSuitValue().ToString();
            }
            else if (!_cell3)
            {
               this.cardImage3.CardValue = card.Value;
               _cell3 = true;
               lblCell3.Text = this.cardImage3.GetFaceValue().ToString() + " of " + this.cardImage3.GetSuitValue().ToString();
            }
            else if (!_cell4)
            {
               this.cardImage4.CardValue = card.Value;
               _cell4 = true;
               lblCell4.Text = this.cardImage4.GetFaceValue().ToString() + " of " + this.cardImage4.GetSuitValue().ToString();
            }
            else if (!_cell5)
            {
               this.cardImage5.CardValue = card.Value;
               _cell5 = true;
               lblCell5.Text = this.cardImage5.GetFaceValue().ToString() + " of " + this.cardImage5.GetSuitValue().ToString();
            }
            else
            {
               ResetCells();
            }
         }
         catch (Exception ex)
         {
            MessageBox.Show(ex.ToString());
         }

      }

      private void ResetCells()
      {
         this._cell1 = false;
         this._cell2 = false;
         this._cell3 = false;
         this._cell4 = false;
         this._cell5 = false;
      }

      private void btnHandTest_Click(object sender, EventArgs e)
      {
         Hand hand = new Hand(GameTypes.DoubleBonus);
         hand.CoinsPlayed = 5;
         hand.CoinValue = .25M;

         hand.Card1 = new Card((byte)CardValues.AceOfClubs);
         this.cardImage1.SetCardValue(hand.Card1.Value);
         lblCell1.Text = this.cardImage1.GetFaceValue().ToString() + " of " + this.cardImage1.GetSuitValue().ToString();

         hand.Card2 = new Card((byte)CardValues.AceOfDiamonds);
         this.cardImage2.SetCardValue(hand.Card2.Value);
         lblCell2.Text = this.cardImage2.GetFaceValue().ToString() + " of " + this.cardImage2.GetSuitValue().ToString();

         hand.Card3 = new Card((byte)CardValues.AceOfHearts);
         this.cardImage3.SetCardValue(hand.Card3.Value);
         lblCell3.Text = this.cardImage3.GetFaceValue().ToString() + " of " + this.cardImage3.GetSuitValue().ToString();

         hand.Card4 = new Card((byte)CardValues.AceOfSpades);
         this.cardImage4.SetCardValue(hand.Card4.Value);
         lblCell4.Text = this.cardImage4.GetFaceValue().ToString() + " of " + this.cardImage4.GetSuitValue().ToString();

         hand.Card5 = new Card((byte)CardValues.EightOfClubs);
         this.cardImage5.SetCardValue(hand.Card5.Value);
         lblCell5.Text = this.cardImage5.GetFaceValue().ToString() + " of " + this.cardImage5.GetSuitValue().ToString();

         PayOut po = hand.Evaluate();

         this.label1.Text = po.PokerHand.ToString();
         this.label2.Text = po.Amount.ToString();
         this.label3.Text = po.Credits.ToString();

      }

      private void Form1_FormClosing(object sender, FormClosingEventArgs e)
      {
         _deck = null;

         cardImage1.Dispose();
         cardImage2.Dispose();
         cardImage3.Dispose();
         cardImage4.Dispose();
         cardImage5.Dispose();
      }
   }
}
